package com.jz.community.moduleshoppingguide.nearshop.ui.view;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Handler;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jz.community.basecomm.bean.ShareInfo;
import com.jz.community.basecomm.reflection.share.ShareActionReflectUtils;
import com.jz.community.moduleshoppingguide.R;
import com.jz.community.moduleshoppingguide.nearshop.bean.ShopInfoBean;
import com.jz.community.sharesdk.share.ShareApi;
import com.zyyoona7.popup.EasyPopup;

import cn.bingoogolapple.qrcode.zxing.QRCodeEncoder;
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class ShareShopPopUp {


    private EasyPopup easyPopup;
    private Context context;
    private ShopInfoBean shopInfoBean;
    private RelativeLayout share_layout;
    private ImageView share_goods_image_iv;
    private TextView tv_share_shop_name;
    private TextView tv_share_shop_zs;
    private TextView tv_share_shop_address;
    private ImageView share_goods_scan_btn;
    private Button save_share_iv;
    private Bitmap bitmap;
    private Bitmap shareBitmap;
    private ShareInfo shareInfo;

    public ShareShopPopUp(Context context) {
        this.context = context;
    }


    public void addShopDetailInfo(ShopInfoBean shopInfoBean, Bitmap bitmap) {
        this.shopInfoBean = shopInfoBean;
        this.bitmap = bitmap;
        initPop(context);
    }

    private void initPop(Context context) {
        easyPopup = EasyPopup.create()
                .setContentView(context, R.layout.share_shop_layout)
                .setAnimationStyle(R.style.RightTop2PopAnim)
                //是否允许点击PopupWindow之外的地方消失
                .setFocusAndOutsideEnable(true)
                //允许背景变暗
                .setBackgroundDimEnable(true)
                //变暗的透明度(0-1)，0为完全透明
                .setDimValue(0.4f)
                .setOnViewListener(view -> initView(view))
                .apply();
    }

    private void initView(View view) {
        share_layout = view.findViewById(R.id.share_layout);
        share_goods_image_iv = view.findViewById(R.id.share_goods_image_iv);
        tv_share_shop_name = view.findViewById(R.id.tv_share_shop_name);
        tv_share_shop_zs = view.findViewById(R.id.tv_share_shop_zs);
        tv_share_shop_address = view.findViewById(R.id.tv_share_shop_address);
        save_share_iv = view.findViewById(R.id.save_share_iv);
        share_goods_scan_btn = view.findViewById(R.id.share_goods_scan_btn);

        share_goods_image_iv.setImageBitmap(bitmap);
        tv_share_shop_name.setText(shopInfoBean.getName());
        tv_share_shop_zs.setText("在售商品" + shopInfoBean.getOnlineGoodsCount() + "件");
        tv_share_shop_address.setText(shopInfoBean.getAddress());

        share_layout.setDrawingCacheEnabled(true);
        share_layout.buildDrawingCache();
        shareInfo = new ShareInfo();
        makeShareGoodEncode(shopInfoBean.getWeChatLink());
        handleShareShopLayout();
        save_share_iv.setOnClickListener(v -> {
            new Handler().postDelayed(() -> easyPopup.dismiss(), 1000);
            ShareActionReflectUtils.jumpSharerPage(context, shareInfo, () -> {
                shareInfo.setLink(shopInfoBean.getWeChatLink());
                shareInfo.setSubject(shopInfoBean.getName());
                shareInfo.setText(shopInfoBean.getAddress());
                shareInfo.setImageUrl(shopInfoBean.getLogo());
                ShareApi.getInstance().shareWeiXinSmallRoutine((Activity) context, shareInfo);
            });
        });
    }


    @SuppressLint("CheckResult")
    private void makeShareGoodEncode(String shopShareScan) {
        Observable.just(shopShareScan)
                .map(s -> QRCodeEncoder.syncEncodeQRCode(s, 320, Color.parseColor("#000000")))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe((Bitmap bm) -> share_goods_scan_btn.setImageBitmap(bm));
    }

    private void handleShareShopLayout() {
        new Handler().postDelayed(() -> {
            shareBitmap = share_layout.getDrawingCache();
            Bitmap thumbBmp = Bitmap.createScaledBitmap(shareBitmap, 480, 800, true);
            shareInfo.setBitmap(thumbBmp);
        }, 100);
    }

    public void showShopSharePopUp(View view) {
        if (easyPopup != null && easyPopup.isShowing()) {
            easyPopup.dismiss();
        } else {
            easyPopup.showAtLocation(view, Gravity.CENTER, 0, 0);
            handleShareShopLayout();
        }
    }

}
